/*
 * Created by the DBAccessor generator.
 * Programmer: Lennart Martens
 * Date: 07/11/2008
 * Time: 10:48:44
 */
package com.compomics.prioritizer.db.accessors;

import com.compomics.util.db.interfaces.*;

import java.sql.*;
import java.io.*;
import java.util.*;

/*
 * CVS information:
 *
 * $Revision: 1.4 $
 * $Date: 2007/07/06 09:41:53 $
 */

/**
 * This class is a generated accessor for the Modification_link table.
 *
 * @author DBAccessor generator class (Lennart Martens).
 */
public class Modification_linkTableAccessor implements Deleteable, Retrievable, Updateable, Persistable {

	/**
	 * This variable tracks changes to the object.
	 */
	protected boolean iUpdated = false;

	/**
	 * This variable can hold generated primary key columns.
	 */
	protected Object[] iKeys = null;

	/**
	 * This variable represents the contents for the 'modification_linkid' column.
	 */
	protected long iModification_linkid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'project' column.
	 */
	protected int iProject = Integer.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'protocol' column.
	 */
	protected int iProtocol = Integer.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'type' column.
	 */
	protected int iType = Integer.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'l_linkid' column.
	 */
	protected long iL_linkid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'l_modificationid' column.
	 */
	protected long iL_modificationid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'username' column.
	 */
	protected String iUsername = null;


	/**
	 * This variable represents the contents for the 'creationdate' column.
	 */
	protected java.sql.Timestamp iCreationdate = null;


	/**
	 * This variable represents the contents for the 'modificationdate' column.
	 */
	protected java.sql.Timestamp iModificationdate = null;


	/**
	 * This variable represents the key for the 'modification_linkid' column.
	 */
	public static final String MODIFICATION_LINKID = "MODIFICATION_LINKID";

	/**
	 * This variable represents the key for the 'project' column.
	 */
	public static final String PROJECT = "PROJECT";

	/**
	 * This variable represents the key for the 'protocol' column.
	 */
	public static final String PROTOCOL = "PROTOCOL";

	/**
	 * This variable represents the key for the 'type' column.
	 */
	public static final String TYPE = "TYPE";

	/**
	 * This variable represents the key for the 'l_linkid' column.
	 */
	public static final String L_LINKID = "L_LINKID";

	/**
	 * This variable represents the key for the 'l_modificationid' column.
	 */
	public static final String L_MODIFICATIONID = "L_MODIFICATIONID";

	/**
	 * This variable represents the key for the 'username' column.
	 */
	public static final String USERNAME = "USERNAME";

	/**
	 * This variable represents the key for the 'creationdate' column.
	 */
	public static final String CREATIONDATE = "CREATIONDATE";

	/**
	 * This variable represents the key for the 'modificationdate' column.
	 */
	public static final String MODIFICATIONDATE = "MODIFICATIONDATE";




	/**
	 * Default constructor.
	 */
	public Modification_linkTableAccessor() {
	}

	/**
	 * This constructor allows the creation of the 'Modification_linkTableAccessor' object based on a set of values in the HashMap.
	 *
	 * @param	aParams	HashMap with the parameters to initialize this object with.
	 *		<i>Please use only constants defined on this class as keys in the HashMap!</i>
	 */
	public Modification_linkTableAccessor(HashMap aParams) {
		if(aParams.containsKey(MODIFICATION_LINKID)) {
			this.iModification_linkid = ((Long)aParams.get(MODIFICATION_LINKID)).longValue();
		}
		if(aParams.containsKey(PROJECT)) {
			this.iProject = ((Integer)aParams.get(PROJECT)).intValue();
		}
		if(aParams.containsKey(PROTOCOL)) {
			this.iProtocol = ((Integer)aParams.get(PROTOCOL)).intValue();
		}
		if(aParams.containsKey(TYPE)) {
			this.iType = ((Integer)aParams.get(TYPE)).intValue();
		}
		if(aParams.containsKey(L_LINKID)) {
			this.iL_linkid = ((Long)aParams.get(L_LINKID)).longValue();
		}
		if(aParams.containsKey(L_MODIFICATIONID)) {
			this.iL_modificationid = ((Long)aParams.get(L_MODIFICATIONID)).longValue();
		}
		if(aParams.containsKey(USERNAME)) {
			this.iUsername = (String)aParams.get(USERNAME);
		}
		if(aParams.containsKey(CREATIONDATE)) {
			this.iCreationdate = (java.sql.Timestamp)aParams.get(CREATIONDATE);
		}
		if(aParams.containsKey(MODIFICATIONDATE)) {
			this.iModificationdate = (java.sql.Timestamp)aParams.get(MODIFICATIONDATE);
		}
		this.iUpdated = true;
	}


	/**
	 * This constructor allows the creation of the 'Modification_linkTableAccessor' object based on a resultset
	 * obtained by a 'select * from Modification_link' query.
	 *
	 * @param	aResultSet	ResultSet with the required columns to initialize this object with.
	 * @exception	SQLException	when the ResultSet could not be read.
	 */
	public Modification_linkTableAccessor(ResultSet aResultSet) throws SQLException {
		this.iModification_linkid = aResultSet.getLong("modification_linkid");
		this.iProject = aResultSet.getInt("project");
		this.iProtocol = aResultSet.getInt("protocol");
		this.iType = aResultSet.getInt("type");
		this.iL_linkid = aResultSet.getLong("l_linkid");
		this.iL_modificationid = aResultSet.getLong("l_modificationid");
		this.iUsername = (String)aResultSet.getObject("username");
		this.iCreationdate = (java.sql.Timestamp)aResultSet.getObject("creationdate");
		this.iModificationdate = (java.sql.Timestamp)aResultSet.getObject("modificationdate");

		this.iUpdated = true;
	}


	/**
	 * This method returns the value for the 'Modification_linkid' column
	 * 
	 * @return	long	with the value for the Modification_linkid column.
	 */
	public long getModification_linkid() {
		return this.iModification_linkid;
	}

	/**
	 * This method returns the value for the 'Project' column
	 * 
	 * @return	int	with the value for the Project column.
	 */
	public int getProject() {
		return this.iProject;
	}

	/**
	 * This method returns the value for the 'Protocol' column
	 * 
	 * @return	int	with the value for the Protocol column.
	 */
	public int getProtocol() {
		return this.iProtocol;
	}

	/**
	 * This method returns the value for the 'Type' column
	 * 
	 * @return	int	with the value for the Type column.
	 */
	public int getType() {
		return this.iType;
	}

	/**
	 * This method returns the value for the 'L_linkid' column
	 * 
	 * @return	long	with the value for the L_linkid column.
	 */
	public long getL_linkid() {
		return this.iL_linkid;
	}

	/**
	 * This method returns the value for the 'L_modificationid' column
	 * 
	 * @return	long	with the value for the L_modificationid column.
	 */
	public long getL_modificationid() {
		return this.iL_modificationid;
	}

	/**
	 * This method returns the value for the 'Username' column
	 * 
	 * @return	String	with the value for the Username column.
	 */
	public String getUsername() {
		return this.iUsername;
	}

	/**
	 * This method returns the value for the 'Creationdate' column
	 * 
	 * @return	java.sql.Timestamp	with the value for the Creationdate column.
	 */
	public java.sql.Timestamp getCreationdate() {
		return this.iCreationdate;
	}

	/**
	 * This method returns the value for the 'Modificationdate' column
	 * 
	 * @return	java.sql.Timestamp	with the value for the Modificationdate column.
	 */
	public java.sql.Timestamp getModificationdate() {
		return this.iModificationdate;
	}

	/**
	 * This method sets the value for the 'Modification_linkid' column
	 * 
	 * @param	aModification_linkid	long with the value for the Modification_linkid column.
	 */
	public void setModification_linkid(long aModification_linkid) {
		this.iModification_linkid = aModification_linkid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Project' column
	 * 
	 * @param	aProject	int with the value for the Project column.
	 */
	public void setProject(int aProject) {
		this.iProject = aProject;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Protocol' column
	 * 
	 * @param	aProtocol	int with the value for the Protocol column.
	 */
	public void setProtocol(int aProtocol) {
		this.iProtocol = aProtocol;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Type' column
	 * 
	 * @param	aType	int with the value for the Type column.
	 */
	public void setType(int aType) {
		this.iType = aType;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'L_linkid' column
	 * 
	 * @param	aL_linkid	long with the value for the L_linkid column.
	 */
	public void setL_linkid(long aL_linkid) {
		this.iL_linkid = aL_linkid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'L_modificationid' column
	 * 
	 * @param	aL_modificationid	long with the value for the L_modificationid column.
	 */
	public void setL_modificationid(long aL_modificationid) {
		this.iL_modificationid = aL_modificationid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Username' column
	 * 
	 * @param	aUsername	String with the value for the Username column.
	 */
	public void setUsername(String aUsername) {
		this.iUsername = aUsername;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Creationdate' column
	 * 
	 * @param	aCreationdate	java.sql.Timestamp with the value for the Creationdate column.
	 */
	public void setCreationdate(java.sql.Timestamp aCreationdate) {
		this.iCreationdate = aCreationdate;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Modificationdate' column
	 * 
	 * @param	aModificationdate	java.sql.Timestamp with the value for the Modificationdate column.
	 */
	public void setModificationdate(java.sql.Timestamp aModificationdate) {
		this.iModificationdate = aModificationdate;
		this.iUpdated = true;
	}



	/**
	 * This method allows the caller to delete the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int delete(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("DELETE FROM modification_link WHERE modification_linkid = ?");
		lStat.setLong(1, iModification_linkid);
		int result = lStat.executeUpdate();
		lStat.close();
		return result;
	}


	/**
	 * This method allows the caller to read data for this
	 * object from a persistent store based on the specified keys.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public void retrieve(Connection aConn, HashMap aKeys) throws SQLException {
		// First check to see whether all PK fields are present.
		if(!aKeys.containsKey(MODIFICATION_LINKID)) {
			throw new IllegalArgumentException("Primary key field 'MODIFICATION_LINKID' is missing in HashMap!");
		} else {
			iModification_linkid = ((Long)aKeys.get(MODIFICATION_LINKID)).longValue()
;		}
		// In getting here, we probably have all we need to continue. So let's...
		PreparedStatement lStat = aConn.prepareStatement("SELECT * FROM modification_link WHERE modification_linkid = ?");
		lStat.setLong(1, iModification_linkid);
		ResultSet lRS = lStat.executeQuery();
		int hits = 0;
		while(lRS.next()) {
			hits++;
			iModification_linkid = lRS.getLong("modification_linkid");
			iProject = lRS.getInt("project");
			iProtocol = lRS.getInt("protocol");
			iType = lRS.getInt("type");
			iL_linkid = lRS.getLong("l_linkid");
			iL_modificationid = lRS.getLong("l_modificationid");
			iUsername = (String)lRS.getObject("username");
			iCreationdate = (java.sql.Timestamp)lRS.getObject("creationdate");
			iModificationdate = (java.sql.Timestamp)lRS.getObject("modificationdate");
		}
		lRS.close();
		lStat.close();
		if(hits>1) {
			throw new SQLException("More than one hit found for the specified primary keys in the 'modification_link' table! Object is initialized to last row returned.");
		} else if(hits == 0) {
			throw new SQLException("No hits found for the specified primary keys in the 'modification_link' table! Object is not initialized correctly!");
		}
	}


	/**
	 * This method allows the caller to update the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int update(Connection aConn) throws SQLException {
		if(!this.iUpdated) {
			return 0;
		}
		PreparedStatement lStat = aConn.prepareStatement("UPDATE modification_link SET modification_linkid = ?, project = ?, protocol = ?, type = ?, l_linkid = ?, l_modificationid = ?, username = ?, creationdate = ?, modificationdate = CURRENT_TIMESTAMP WHERE modification_linkid = ?");
		lStat.setLong(1, iModification_linkid);
		lStat.setInt(2, iProject);
		lStat.setInt(3, iProtocol);
		lStat.setInt(4, iType);
		lStat.setLong(5, iL_linkid);
		lStat.setLong(6, iL_modificationid);
		lStat.setObject(7, iUsername);
		lStat.setObject(8, iCreationdate);
		lStat.setLong(9, iModification_linkid);
		int result = lStat.executeUpdate();
		lStat.close();
		this.iUpdated = false;
		return result;
	}


	/**
	 * This method allows the caller to insert the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int persist(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("INSERT INTO modification_link (modification_linkid, project, protocol, type, l_linkid, l_modificationid, username, creationdate, modificationdate) values(?, ?, ?, ?, ?, ?, CURRENT_USER, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)");
		if(iModification_linkid == Long.MIN_VALUE) {
			lStat.setNull(1, 4);
		} else {
			lStat.setLong(1, iModification_linkid);
		}
		if(iProject == Integer.MIN_VALUE) {
			lStat.setNull(2, -6);
		} else {
			lStat.setInt(2, iProject);
		}
		if(iProtocol == Integer.MIN_VALUE) {
			lStat.setNull(3, -6);
		} else {
			lStat.setInt(3, iProtocol);
		}
		if(iType == Integer.MIN_VALUE) {
			lStat.setNull(4, -6);
		} else {
			lStat.setInt(4, iType);
		}
		if(iL_linkid == Long.MIN_VALUE) {
			lStat.setNull(5, 4);
		} else {
			lStat.setLong(5, iL_linkid);
		}
		if(iL_modificationid == Long.MIN_VALUE) {
			lStat.setNull(6, 4);
		} else {
			lStat.setLong(6, iL_modificationid);
		}
		int result = lStat.executeUpdate();

		// Retrieving the generated keys (if any).
		ResultSet lrsKeys = lStat.getGeneratedKeys();
		ResultSetMetaData lrsmKeys = lrsKeys.getMetaData();
		int colCount = lrsmKeys.getColumnCount();
		iKeys = new Object[colCount];
		while(lrsKeys.next()) {
			for(int i=0;i<iKeys.length;i++) {
				iKeys[i] = lrsKeys.getObject(i+1);
			}
		}
		lrsKeys.close();
		lStat.close();
		this.iUpdated = false;
		return result;
	}

	/**
	 * This method will return the automatically generated key for the insert if 
	 * one was triggered, or 'null' otherwise.
	 *
	 * @return	Object[]	with the generated keys.
	 */
	public Object[] getGeneratedKeys() {
		return this.iKeys;
	}

}